﻿//using System;
//using System.Collections.Generic;
//using System.Text;
//using DCRF.Common.Interface;
//using DCRF.Common.Primitive;
//using DCRF.Common.Collection;
//using System.Threading;
//using System.Collections;
//using DCRF.Common.Helper;
//using DCRF.Common.Definition;
//using DCRF.Common.Support;
//using DCRF.Common.Connection;

//namespace DCRF.Common.Distribution
//{
//    /// <summary>
//    /// This is a wrapper class just to make things more simple and prevent adding a lot of methods
//    /// to BlockWeb class
//    /// </summary>
//    public class LocalBlockAccessor: IBlockAccessor
//    {
//        #region private fields

//        private IBlock Block = null;
//        private Guid handle = Guid.Empty;

//        #endregion

//        #region constructor

//        public LocalBlockAccessor(IBlock cmp, Guid h)
//        {
//            Block = cmp;
//            handle = h;
//        }

//        #endregion

//        #region Info

//        public BlockInfo Info
//        {
//            get
//            {
//                return Block.BlockInfo;
//            }
//        }

//        public Guid Id
//        {
//            get
//            {
//                return Block.Id;
//            }
//        }

//        public IBlockAccessor this[Guid id]
//        {
//            get
//            {
//                return null;
//                //return Block.GetInnerWebBlock(id);
//            }
//        }

//        #endregion

//        #region Connector

//        public Connector this[string key]
//        {
//            get
//            {
//                return Block[key];
//            }
//        }

//        #endregion

//        #region Service

//        public object ProcessRequest(string serviceName, params object[] args)
//        {
//            return innerProcessRequest(serviceName, args);
//        }

//        public void ProcessRequest(string serviceName, RequestCallbackDelegate callBack, object tag, params object[] args)
//        {
//            ArrayList arr = new ArrayList();
//            arr.Add(serviceName);
//            arr.Add(args);
//            arr.Add(callBack);
//            arr.Add(tag);

//            ThreadPool.QueueUserWorkItem(delegate(object state)
//            {
//                ArrayList darr = state as ArrayList;
//                string dServiceName = (string)darr[0];
//                object[] dargs = darr[1] as object[];

//                object result = innerProcessRequest(serviceName, args);

//                RequestCallbackDelegate dCallBack = arr[2] as RequestCallbackDelegate;

//                if (dCallBack != null)
//                {
//                    object dTag = arr[3];

//                    dCallBack(dTag, result);
//                }
//            }, arr);
//        }

//        public T ProcessRequest<T>(string serviceName, params object[] args)
//        {
//            return (T)ProcessRequest(serviceName, args);
//        }

//        private void checkEventCall(ConnectorCollection events, string eventKey, params object[] args)
//        {
//            if (events.Contains(eventKey))
//            {
//                events[eventKey].Raise(args);
//            }
//        }

//        protected object innerProcessRequest(string serviceName, object[] args)
//        {
//            IBlock comp = this.Block;
//            //args: when only 1 argument exists and its null this array becomes null. we need it
//            //to be an array with a single null member for reflection operations
//            if (args == null)
//            {
//                args = new object[1] { null };
//            }

//            Boolean cancelOperation = false;            
//            object response = null;

//            //TODO: we have to prevent infinite loop here
//            //checkEventCall(Block.BlockWeb.GlobalConnectors, EventCode.ProcessingRequest,
//            //    handle, serviceName, args, new ValueTypeHolder<object>(ref response), new ValueTypeHolder<bool>(ref cancelOperation));

//            //if (cancelOperation)
//            //{
//            //    return response;
//            //}

//            //checkEventCall(Block.Connectors, EventCode.ProcessingRequest,
//            //    handle, serviceName, args, new ValueTypeHolder<object>(ref response), new ValueTypeHolder<bool>(ref cancelOperation));

//            //if (cancelOperation)
//            //{
//            //    return response;
//            //}

//            object result = null;

//            try
//            {
//                result = Block.ProcessRequest(serviceName, args);
//            }
//            catch (System.Exception exc)
//            {
//                bool throwException = true;

//                try
//                {
//                    //if this connector did not exist, nothing will happen, only an empty connector will be added
//                    Block[EventCode.ExceptionOccured].Raise(exc, new ValueTypeHolder<bool>(ref throwException));
//                }
//                catch (System.Exception exc2)
//                {
//                    throw exc2;
//                }

//                if (throwException) throw exc;
//            }

//            //checkEventCall(Block.BlockWeb.GlobalConnectors, EventCode.RequestProcessed,
//            //    handle, serviceName, args, new ValueTypeHolder<object>(ref result));

//            //checkEventCall(Block.Connectors, EventCode.RequestProcessed,
//            //    handle, serviceName, args, new ValueTypeHolder<object>(ref result));
            
//            return result;
//        }

//        #endregion
//    }
//}
